package com.whiteape.book.presenter;


import android.os.storage.StorageManager;

import com.whiteape.book.contract.SearchContract;
import com.whiteape.book.core.manager.MediaStorageManager;
import com.whiteape.book.core.manager.WebMediaManager;
import com.whiteape.book.core.manager.WebRequestManager;
import com.whiteape.book.model.zssq.remote.RemoteRepository;
import com.whiteape.book.ui.base.RxPresenter;
import com.whiteape.book.utils.LogUtils;
import com.whiteape.book.utils.RxUtils;

import io.reactivex.disposables.Disposable;

/**
 * Created by newbiechen on 17-6-2.
 */

public class SearchPresenter extends RxPresenter<SearchContract.View>
        implements SearchContract.Presenter {

    @Override
    public void searchHotWord() {
        Disposable disp = RemoteRepository.getInstance()
                .getHotWords()
                .compose(RxUtils::toSimpleSingle)
                .subscribe(
                        bean -> {
                            mView.finishHotWords(bean);
                        },
                        e -> {
                            LogUtils.e(e);
                        }
                );
        addDisposable(disp);
    }

    @Override
    public void searchKeyWord(String query) {
        //TODO 不用追书神器api

        Disposable disp = RemoteRepository.getInstance()
                .getKeyWords(query)
                .compose(RxUtils::toSimpleSingle)
                .subscribe(
                        bean -> {
                            mView.finishKeyWords(bean);
                        },
                        e -> {
                            LogUtils.e(e);
                        }
                );
        addDisposable(disp);
    }

    @Override
    public void searchBook(String query) {
        addDisposable(MediaStorageManager.getInstance().getUsedSourceCount()
                .compose(RxUtils::toSimpleSingle)
                .subscribe(integer -> mView.setUsedSourceCount(integer)));

        Disposable disp = WebMediaManager.getInstance().getSearchInfo(query)
                .compose(RxUtils::toSimpleSingle)
                .subscribe(beans -> {
                    mView.finishBooks(beans);
                }, e -> {
                    LogUtils.e(e);
                    mView.errorBooks();
                });
//        Disposable disp = RemoteRepository.getInstance()
//                .getSearchBooks(query)
//                .compose(RxUtils::toSimpleSingle)
//                .subscribe(
//                        bean -> {
//                            mView.finishBooks(bean);
//                        },
//                        e -> {
//                            LogUtils.e(e);
//                            mView.errorBooks();
//                        }
//                );
        addDisposable(disp);
    }
}
